/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DLManagerAccount.java
 *
 * Created on Sep 6, 2010, 4:33:11 PM
 */

package Gui;

import BusinessLogic.AccountManagement;
import BusinessLogic.BackGroundUtility;
import DTO.Manager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class DLManagerAccount extends javax.swing.JDialog {

    private AccountManagement accma;
    private boolean flag = false;
    private Vector<Manager> listManager;
    private DefaultTableModel model;
    private int index = 0;
    /** Creates new form DLManagerAccount */
    public DLManagerAccount(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        //set background
        ImagePanel iPanel = new ImagePanel(BackGroundUtility.getImage());
        getContentPane().add(iPanel);
        //setlocation for application
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        Dimension dimApp = getSize();
        int pointX = (dim.width - dimApp.width)/2;
        int pointY = (dim.height - dimApp.height)/2 - 10;
        setLocation(pointX, pointY);

        accma = new AccountManagement();
        
        loadManager();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        pnBackground = new javax.swing.JPanel();
        pnContent = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnPrev = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbManager = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lblUser = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        lblUser1 = new javax.swing.JLabel();
        txtPass = new javax.swing.JPasswordField();
        lblUser2 = new javax.swing.JLabel();
        lblUser3 = new javax.swing.JLabel();
        lblUser4 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        cbPower = new javax.swing.JComboBox();
        rbJoin = new javax.swing.JRadioButton();
        rbNotJoin = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        pnBackground.setOpaque(false);

        pnContent.setBackground(new java.awt.Color(255, 255, 255));

        jPanel3.setLayout(null);

        btnPrev.setText("Prev");
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });
        jPanel3.add(btnPrev);
        btnPrev.setBounds(50, 10, 70, 23);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        jPanel3.add(btnNext);
        btnNext.setBounds(140, 10, 70, 23);

        jButton3.setText("First");
        jPanel3.add(jButton3);
        jButton3.setBounds(230, 10, 70, 23);

        jButton4.setText("Last");
        jPanel3.add(jButton4);
        jButton4.setBounds(320, 10, 70, 23);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel3.add(btnSave);
        btnSave.setBounds(50, 50, 70, 23);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel3.add(btnAdd);
        btnAdd.setBounds(140, 50, 70, 23);

        jButton7.setText("Del");
        jPanel3.add(jButton7);
        jButton7.setBounds(230, 50, 70, 23);

        jButton8.setText("Cancel");
        jPanel3.add(jButton8);
        jButton8.setBounds(320, 50, 70, 23);

        tbManager.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbManager.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbManagerMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbManager);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Information Of Account Manager"));
        jPanel1.setLayout(null);

        lblUser.setText("UserName ");
        jPanel1.add(lblUser);
        lblUser.setBounds(50, 20, 68, 24);
        jPanel1.add(txtUser);
        txtUser.setBounds(180, 20, 240, 20);

        lblUser1.setText("Password ");
        jPanel1.add(lblUser1);
        lblUser1.setBounds(50, 50, 68, 24);
        jPanel1.add(txtPass);
        txtPass.setBounds(180, 50, 168, 20);

        lblUser2.setText("Name");
        jPanel1.add(lblUser2);
        lblUser2.setBounds(50, 80, 70, 24);

        lblUser3.setText("Power");
        jPanel1.add(lblUser3);
        lblUser3.setBounds(50, 110, 68, 24);

        lblUser4.setText("Status");
        jPanel1.add(lblUser4);
        lblUser4.setBounds(50, 150, 68, 24);
        jPanel1.add(txtName);
        txtName.setBounds(180, 80, 240, 20);

        cbPower.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrator", "Manager" }));
        jPanel1.add(cbPower);
        cbPower.setBounds(180, 120, 90, 20);

        buttonGroup1.add(rbJoin);
        rbJoin.setText("JOIN");
        jPanel1.add(rbJoin);
        rbJoin.setBounds(180, 150, 93, 23);

        buttonGroup1.add(rbNotJoin);
        rbNotJoin.setSelected(true);
        rbNotJoin.setText("NOT JOIN");
        rbNotJoin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbNotJoinActionPerformed(evt);
            }
        });
        jPanel1.add(rbNotJoin);
        rbNotJoin.setBounds(270, 150, 73, 23);

        javax.swing.GroupLayout pnContentLayout = new javax.swing.GroupLayout(pnContent);
        pnContent.setLayout(pnContentLayout);
        pnContentLayout.setHorizontalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnContentLayout.setVerticalGroup(
            pnContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnContentLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout pnBackgroundLayout = new javax.swing.GroupLayout(pnBackground);
        pnBackground.setLayout(pnBackgroundLayout);
        pnBackgroundLayout.setHorizontalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnBackgroundLayout.setVerticalGroup(
            pnBackgroundLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnBackgroundLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnContent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(pnBackground, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void clearText()
    {
        txtPass.setText("");
        txtName.setText("");        
    }

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // TODO add your handling code here:
        
        
}//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // TODO add your handling code here:
        
 
}//GEN-LAST:event_btnNextActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        String User = txtUser.getText();
        String Name = txtName.getText();
        char[] PassWord = txtPass.getPassword();
        String Pass = String.valueOf(PassWord);        
        int num = cbPower.getSelectedIndex();
        num += 1;
        int confirm = 0;
        if(rbJoin.isSelected()){
            confirm = 1;
        }
        Manager m = new Manager(User, Pass, Name,num, confirm);
        if(!flag)
        {
            accma.update(m);
        }
        else
        {
            accma.insert(m);
        }
        loadManager();
        

}//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        //call method clearText()
        clearText();
        txtUser.setText("");
        flag = true;
        
}//GEN-LAST:event_btnAddActionPerformed

    private void tbManagerMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbManagerMousePressed
        // TODO add your handling code here:
        
}//GEN-LAST:event_tbManagerMousePressed

    private void rbNotJoinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbNotJoinActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbNotJoinActionPerformed

    public void setModel(Vector<Manager> list){
        model = new DefaultTableModel(null, new Object[]{"UserName", "Pass", "Name", "Power", "Status"});
        tbManager.setModel(model);

        for(Manager e : list){
            Vector rowData = new Vector();
            rowData.add(e.getUserName());
            rowData.add(e.getPassWord());
            rowData.add(e.getName());
            rowData.add(e.getPower());
            rowData.add(e.getStatus());
            model.addRow(rowData);
        }
    }

    public void loadManager(){
        listManager = accma.loadManager();
        //display table
        setModel(listManager);
        //index = 0
        index = 0;
        //Display a Zone
        displayObject(index);
        
    }

    private void displayObject(int index){
        Manager m = accma.getByIndex(index);
        
        //set values for textfiels
        txtUser.setText(m.getUserName());
        txtPass.setText(m.getPassWord());
        txtName.setText(m.getName());        
        if(m.getStatus() == 1){
            rbJoin.setSelected(true);
        }else{
            rbNotJoin.setSelected(true);
        }

        


}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JButton btnSave;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbPower;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblUser;
    private javax.swing.JLabel lblUser1;
    private javax.swing.JLabel lblUser2;
    private javax.swing.JLabel lblUser3;
    private javax.swing.JLabel lblUser4;
    private javax.swing.JPanel pnBackground;
    private javax.swing.JPanel pnContent;
    private javax.swing.JRadioButton rbJoin;
    private javax.swing.JRadioButton rbNotJoin;
    private javax.swing.JTable tbManager;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables

}
